package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.Pet;
import service.PetService;
import service.impl.PetServiceImpl;

public class AddServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		Pet pet = new Pet();
		pet.setPetName(request.getParameter("petName"));
		pet.setPetBreed(Integer.parseInt(request.getParameter("petBreed")));
		pet.setPetSex(Integer.parseInt(request.getParameter("petSex")));
		pet.setBirthday(request.getParameter("birthday"));
		pet.setDescription(request.getParameter("description"));
		PetService service = new PetServiceImpl();
		int result = service.add(pet);
		if (result>0) {
			out.print("<script>alert('添加成功！');location.href='ListServlet';</script>");
		}else {
			out.print("<script>alert('添加失败！');history.back();</script>");
		}
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
